package org.csu.cms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import org.csu.cms.dao.AccountMapper;
import org.csu.cms.dao.OrderMapper;
import org.csu.cms.domain.Account;
import org.csu.cms.domain.Order;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
@MapperScan("org.csu.cms.dao")
public class OrderFormService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AccountMapper accountMapper;

    public String createOrderNumber() {//生成订单号
        return String.valueOf(System.currentTimeMillis());

    }

    public boolean addOrder(Order order) {//新增订单
        order.setOrderNumber(createOrderNumber());
        orderMapper.insert(order);
        return true;
    }

    public List<Order> getOrderForm() {
        return orderMapper.selectList(null);
    }//获取所有订单

    public HashMap getDetailedOrder(String orderNumber) {//获取订单详情
        QueryWrapper<Order> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("order_number", orderNumber);
        Order order = orderMapper.selectOne(queryWrapper1);

        QueryWrapper<Account> queryWrapper2 = new QueryWrapper<>();
        String userid = order.getAccountId();
        queryWrapper2.eq("userid", "j2ee");
        Account account = accountMapper.selectOne(queryWrapper2);

        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("orderNmuber", order.getOrderNumber());
        hashMap.put("accountId", account.getUserid());
        return hashMap;

    }

    public boolean deleteOrder(String orderNumber) {//删除订单
        Order order = orderMapper.selectById(orderNumber);
        if (order != null) {
            orderMapper.deleteById(orderNumber);
            return true;
        } else {
            return false;
        }

    }

    public boolean sendGoods(String orderNumber) {//发货

        Order order = orderMapper.selectById(orderNumber);
        if (order.getState().equals("未发货")) {
            order.setState("已发货");
            orderMapper.updateById(order);
            return true;
        } else {
            return false;
        }
    }

    public boolean updateOrder(Order order) {//更改订单
        Order orderEntity = orderMapper.selectById(order.getOrderNumber());
        if (orderEntity != null) {
            orderEntity.setProductName(order.getProductName());
            orderEntity.setQuantity(order.getQuantity());
            orderEntity.setRemarks(order.getRemarks());
            orderMapper.updateById(orderEntity);
            return true;
        } else {
            return false;
        }
    }

    public List<Order> Search(String context) {//查找
        boolean isdigit = true;
        try {
            Integer.parseInt(context);
        } catch (Exception e) {
            isdigit = false;
        }

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        if (isdigit) {
            queryWrapper.like("order_number", context)
                    .or().like("account_id", context)
                    .or().like("product_name", context)
                    .or().like("state", context)
                    .or().like("remarks", context)
                    .or().eq("quantity", Integer.parseInt(context));

        } else {
            queryWrapper.like("order_number", context)
                    .or().like("account_id", context)
                    .or().like("product_name", context)
                    .or().like("state", context)
                    .or().like("remarks", context);
        }

        List<Order> list = orderMapper.selectList(queryWrapper);

        return list;


    }

}
